//
//  WebViewController.m
//  UITest2
//
//  Created by 夏宇彬 on 15/9/19.
//  Copyright © 2015年 夏宇彬. All rights reserved.
//

#import "WebViewController.h"

@interface WebViewController ()
@property (weak, nonatomic) IBOutlet UISegmentedControl *colorChoice;
@property (weak, nonatomic) IBOutlet UIWebView *flowerView;
@property (weak, nonatomic) IBOutlet UIWebView *flowerDetailView;

- (IBAction)getFlower:(id)sender;
- (IBAction)toggleFlowerDetail:(id)sender;
- (IBAction)doReturn:(id)sender;

@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)getFlower:(id)sender {
    NSURL* imageURL;
    NSURL* detailURL;
    NSString* imageURLString;
    NSString* detailURLString;
    NSString* color;
    int sessionID;
    
    //分段控件选择的颜色
    color = [self.colorChoice titleForSegmentAtIndex: self.colorChoice.selectedSegmentIndex];
    sessionID = random()%50000;
    
    //创建网址字符串
    imageURLString = [NSString stringWithFormat:@"http://www.floraphotographs.com/showrandomios.php?color=%@&session=%d",color,sessionID];
    detailURLString = [NSString stringWithFormat:@"http://www.floraphotographs.com/detailios.php?session=%d",sessionID];
    //生成网址
    imageURL = [NSURL URLWithString:imageURLString];
    detailURL = [NSURL URLWithString:detailURLString];
    
    //向服务器发送请求
    [self.flowerView loadRequest:[NSURLRequest requestWithURL:imageURL]];
    [self.flowerDetailView loadRequest:[NSURLRequest requestWithURL:detailURL]];
}

- (IBAction)toggleFlowerDetail:(id)sender {
    self.flowerDetailView.hidden = ![sender isOn];
}

- (IBAction)doReturn:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
